﻿using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace HTR.Model
{
    public class CamaraInspeccion : IDibujable
    {

        #region Propiedades

        private Circle _acCircle;
        public Point3d Centro
        {
            get { return this._acCircle.Center; }
        }

        private DBText _textoCI;

        private string _valorCotaTerreno;
        public string ValorCotaTerreno
        {
            get { return this._valorCotaTerreno; }
            set { this._valorCotaTerreno = value; }
        }

        private CotaTerreno _cotaTerreno;
        public CotaTerreno CotaTerreno
        {
            get { return this._cotaTerreno; }
            private set { this._cotaTerreno = value; }
        }

        public string Handle
        {
            get { return _acCircle.Handle.ToString(); }
        }

        #endregion // Propiedades


        #region Constructores

        public CamaraInspeccion(Point3d centro, string valorCotaTerreno)
        {
            this._acCircle = new Circle();
            this._acCircle.SetDatabaseDefaults();
            this._acCircle.Center = centro;
            this._acCircle.Radius = 2;

            this._textoCI = new DBText();
            this._textoCI.TextString = "CI";
            this._textoCI.Justify = AttachmentPoint.MiddleCenter;
            this._textoCI.Position = centro;
            this._textoCI.AlignmentPoint = centro;
            this._textoCI.Height = 2;

            this._valorCotaTerreno = valorCotaTerreno;
            if (valorCotaTerreno == string.Empty)
                this._cotaTerreno = null;
            else
                this._cotaTerreno = new CotaTerreno(centro, this._valorCotaTerreno, false);
        }

        #endregion // Constructores


        #region Metodos

        public void Dibujar()
        {
            try
            {
                Document acDoc = Application.DocumentManager.MdiActiveDocument;
                Database acCurDb = acDoc.Database;
                using (DocumentLock docLock = acDoc.LockDocument())
                {
                    // Comienzo una transacción
                    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                    {
                        // Abro la Layer table para lectura
                        LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                        // Verifico si existe la capa de Camaras de Inspeccion y sino la creo
                        string layerName = "Camara de Inspeccion@CamaraInspeccion";
                        if (!acLyrTbl.Has(layerName))
                        {
                            LayerTableRecord acLyrTblRec = new LayerTableRecord();
                            acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 11);
                            acLyrTblRec.Name = layerName;

                            // Actualizo la Layer table para escritura
                            acLyrTbl.UpgradeOpen();

                            // Agrego la nueva capa a la Layer table y a la transacción
                            acLyrTbl.Add(acLyrTblRec);
                            acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);

                        }
                        // Abro la Block table para lectura
                        BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                        // Abro el Block table record Model space para escritura
                        BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                        this._acCircle.Layer = layerName;
                        acBlkTblRec.AppendEntity(this._acCircle);
                        acTrans.AddNewlyCreatedDBObject(this._acCircle, true);
                        this._textoCI.Layer = layerName;
                        acBlkTblRec.AppendEntity(this._textoCI);
                        acTrans.AddNewlyCreatedDBObject(this._textoCI, true);
                        if (CotaTerreno != null) CotaTerreno.Dibujar(acBlkTblRec, acTrans, acLyrTbl, "Cota Terreno Camara Inspeccion@CotaTerrenoCI");
                        // Guardo los cambios y termino la transacción
                        acTrans.Commit();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Modificar(string NuevaCotaTerreno)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (DocumentLock docLock = acDoc.LockDocument())
            {
                // Comienzo una transacción
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // Abro la Layer table para lectura
                    LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                    // Abro la Block table para lectura
                    BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                    // Abro el Block table record Model space para escritura
                    BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    if (NuevaCotaTerreno != string.Empty)
                        if (CotaTerreno != null)
                            CotaTerreno.Modificar(acTrans, NuevaCotaTerreno);
                        else
                        {
                            CotaTerreno = new CotaTerreno(this._acCircle.Center, NuevaCotaTerreno, false);
                            CotaTerreno.Dibujar(acBlkTblRec, acTrans, acLyrTbl, "Cota Terreno Camara Inspeccion@CotaTerrenoCI");

                        }
                    else
                    {
                        CotaTerreno.Borrar(acTrans);
                        CotaTerreno = null;
                    }
                    ValorCotaTerreno = NuevaCotaTerreno;
                    acTrans.Commit();
                }
            }
            acDoc.Editor.Regen();
        }

        #endregion // Metodos

    }

    public class CamaraInspeccionCollection : Dictionary<string, CamaraInspeccion> { }

}
